﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using LinkedInTestApp.Annotations;
using LinkedInWinRT.Client;
using LinkedInWinRT.Helpers.Models;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;


namespace LinkedInTestApp
{
    public sealed partial class MainPage : INotifyPropertyChanged
    {
        private LinkedInClient _linkedInClient;
        private List<Person> _connections;

        public MainPage()
        {
            InitializeComponent();
        }

        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {

        }

        public List<Person> Connections
        {
            get { return _connections; }
            set
            {
                if (Equals(value, _connections)) return;
                _connections = value;
                OnPropertyChanged();
            }
        }

        private async void OnLoginClick(object sender, RoutedEventArgs e)
        {
            _linkedInClient = new LinkedInClient("e2gj7h3o5qvy", "qpfp18NPZ2FUZJdD", "http://google.com");
            var permissions = new List<Permission>();
            permissions.Add(Permission.BasicProfile);
            permissions.Add(Permission.FullProfile);
            permissions.Add(Permission.EmailAddress);
            permissions.Add(Permission.Network);
            permissions.Add(Permission.Updates);
            var loggedIn = await _linkedInClient.LoginAsync(permissions);
            if (loggedIn == false)
            {
                // Login failed
            }
            else
            {
                // Login succeeded
                //string updates = await _linkedInClient.GetUpdates();
                //await new MessageDialog(updates).ShowAsync();
                await _linkedInClient.RefreshUserInfoAsync();
                Connections = _linkedInClient.AuthenticatedUser.Connections;
                ConnectionsList.ItemsSource = Connections;
            }
        }

        private void LoadAuthenticatedUserProfile(object sender, RoutedEventArgs e)
        {
            var person = _linkedInClient.AuthenticatedUser;
            LoadUser(person);
        }

        private void LoadUser(Person person)
        {
            try
            {
                txtProfile.Text = "First name:" + person.FirstName;
                txtProfile.Text += "\nLast name: " + person.LastName;
                txtProfile.Text += "\nId: " + person.Id;
                txtProfile.Text += "\nEmail: " + person.EmailAddress;
                txtProfile.Text += "\nProfile Url: " + person.ProfileUrl;
                txtProfile.Text += "\nPicture Url: " + person.PictureUrl;
                var image = new BitmapImage(new Uri(person.PictureUrl));
                profilePicture.Source = image;
            }
            catch (Exception)
            {
                
            }
        }

        private void OnGetEmail(object sender, RoutedEventArgs e)
        {
            txtProfile.Text = _linkedInClient.AuthenticatedUser.EmailAddress;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        private void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            var handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }

        private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var person = ConnectionsList.SelectedItem as Person;
            if (person != null)
            {
                LoadUser(person);
            }
        }

        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            ApiResult.Text = await _linkedInClient.ApiRequestAsync(ApiUri.Text);
        }
    }
}
